package com.atguigu.lease.web.admin.custom.bloom;

/*
*功能：
 作者：chenhao
*日期： 2025/3/21 上午10:17
*/
import com.atguigu.lease.common.bloomFilter.BloomFilterConfig;
import com.atguigu.lease.common.bloomFilter.HashStrategy;
import com.atguigu.lease.common.bloomFilter.HashStrategyFactory;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.RoomInfoService;


import jakarta.annotation.PostConstruct;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/*
*功能：
 作者：chenhao
*日期： 2025/3/21 上午10:16
*/
@Component
public class BloomFilterInit {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    private BloomFilterConfig bloomFilter;

    public BloomFilterConfig getBloomFilter() {
        return bloomFilter;
    }

    @PostConstruct
    public void initBloomFilter() {
        HashStrategy strategy = HashStrategyFactory.getStrategy("simple");
        bloomFilter = new BloomFilterConfig(100000, strategy);

        // 加载已有数据
        List<String> ids = new ArrayList<>();
        roomInfoMapper.selectList(null).forEach(item->{
            ids.add(item.getId().toString());
        });// 查询已有房源ID
        ids.forEach(bloomFilter::add);

        System.out.println("布隆过滤器初始化完成，房源数量：" + ids.size());
    }
}
